<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-tabs>
        <!-- 角色管理 -->
        <el-tab-pane label="角色管理">
          <!-- 新增角色按钮 -->
          <el-row style="height: 60px">
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="isShow = true"
            >新增角色</el-button>
          </el-row>
          <!-- 表格 -->
          <el-table border stripe :data="list">
            <el-table-column label="序号" width="120" type="index" />
            <el-table-column label="角色名称" width="240" prop="name" />
            <el-table-column label="描述" prop="description" />
            <el-table-column label="操作">
              <!-- 作用域插槽 -->
              <template slot-scope="scope">
                <el-button size="small" type="success">分配权限</el-button>
                <el-button size="small" type="primary" @click="editHandler(scope.row)">编辑</el-button>
                <el-button
                  size="small"
                  type="danger"
                  @click="delHandler(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页组件 -->
          <el-row
            type="flex"
            justify="center"
            align="middle"
            style="height: 60px"
          >
            <!-- 分页组件 -->
            <el-pagination
              :current-page="queryData.page"
              :page-sizes="[2, 3, 4, 5]"
              :page-size="queryData.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-row>
        </el-tab-pane>
        <!-- 公司信息 -->
        <el-tab-pane label="公司信息">
          <el-alert
            title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
            type="info"
            show-icon
            :closable="false"
          />
          <el-form label-width="120px" style="margin-top: 50px">
            <el-form-item label="公司名称">
              <el-input
                v-model="companyData.name"
                disabled
                style="width: 400px"
              />
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input
                v-model="companyData.companyAddress"
                disabled
                style="width: 400px"
              />
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input
                v-model="companyData.mailbox"
                disabled
                style="width: 400px"
              />
            </el-form-item>
            <el-form-item label="备注">
              <el-input
                v-model="companyData.remarks"
                type="textarea"
                :rows="3"
                disabled
                style="width: 400px"
              />
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <!-- 弹框，增加/修改角色 -->
      <el-dialog
        :title="title"
        :visible.sync="isShow"
        width="50%"
        @close="close"
      >
        <el-form
          ref="form"
          :model="formData"
          label-width="140px"
          :rules="formRules"
        >
          <el-form-item label="角色名称" required>
            <el-input v-model="formData.name" prop="name" />
          </el-form-item>
          <el-form-item label="角色描述" required>
            <el-input v-model="formData.description" prop="name" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="isShow = false">取 消</el-button>
          <el-button type="primary" @click="btnOK">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getRoleInfo, getRoleListData, getCompanyInfo, delRoleInfo, addRoleInfo, editRoleInfo } from '@/api/settings'
export default {
  name: 'Settings',
  data() {
    return {
      currentPage4: 1,
      queryData: {
        page: 1,
        pagesize: 2
      },
      list: [],
      total: 0, // 总数
      companyData: {},
      isShow: false, // 弹框控件
      formData: { // 表单变量
        name: '',
        description: ''
      },
      formRules: { // 表单验证
        name: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改' : '添加'
    }
  },
  created() {
    this.initData()
    this.getCompanyInfo()
  },
  methods: {
    // 获取角色列表
    async initData() {
      const { rows, total } = await getRoleListData(this.queryData)
      console.log(rows)
      this.list = rows
      this.total = total
    },
    // 获取公司列表
    async getCompanyInfo() {
      const res = await getCompanyInfo(this.$store.getters.companyId)
      console.log(res)
      this.companyData = res
    },
    // 删除角色列表
    async delHandler(val) {
      await this.$confirm('确定删除吗？')
      await delRoleInfo(val.id)
      this.$message.success('删除成功')
      // 不是第一页，且该页面只有一个元素，且删除成功；页码-1，获取数据
      if (this.queryData.page !== 1 && this.list.length === 1) {
        this.queryData.page--
      }
      this.initData()
    },
    handleSizeChange(val) {
      // val是每页最新数量
      this.queryData.pagesize = val
      this.initData()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      // val就是最新页码
      this.queryData.page = val
      this.initData()
      console.log(`当前页: ${val}`)
    },
    // 新增/修改角色
    async btnOK() {
      // 只有校验通过，才会执行await下方的内容
      try {
        await this.$refs.form.validate()
        if (this.formData.id) {
          // 编辑
          await editRoleInfo(this.formData)
        } else {
          // 新增
          await addRoleInfo(this.formData)
        }
        // 重新拉去数据
        this.initData()
        this.$message.success('操作成功')
        this.isShow = false
      } catch (error) {
        console.log(error)
      }
    },
    // 关闭清空弹框,清空内容
    close() {
      this.fromData = {}
      this.$refs.form.clearValidate()
      this.isShow = false
    },
    // 修改点击事件，获取改行数据
    async editHandler(row) {
      const res = await getRoleInfo(row.id)
      this.formData = res
      this.isShow = true
    }
  }
}
</script>

<style lang="scss" scoped></style>
